package com.yjd.bi.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.enums.UserEnums;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.report.util.TimeDimensionUtil;
import com.yjd.bi.user.model.BasicDataAppModel;
import com.yjd.bi.user.service.IBasicDataAppService;
import com.yjd.bi.user.service.IBasicDataPcService;
import com.yjd.bi.user.dao.IBasicDataAppDao;

/**
 * <b>description</b>：app基础数据业务实现<br>
 * <b>time</b>：2016-06-06 15:18:27 <br>
 * <b>author</b>：  zhoujin@yijiedai.com
 */
@Service("basicDataAppService")
public class BasicDataAppServiceImpl extends BaseServiceImpl implements IBasicDataAppService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public BasicDataAppModel insert(BasicDataAppModel model) throws Exception {
		return this.basicDataAppDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(BasicDataAppModel model) throws Exception {
		return this.basicDataAppDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.basicDataAppDao.deleteModel(paramMap, true);
	}

	@Resource
	private IBasicDataAppDao basicDataAppDao;
	
	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IBasicDataPcService basicDataPcService;

	@Override
	public IBaseDao getBaseDao() {
		return this.basicDataAppDao;
	}
	
	@Override
	public EChartsModel getDates(Integer type,TimeType timeType, Long timeFrom, String field)
			throws Exception {
		// TODO Auto-generated method stub
		EChartsModel ec = new EChartsModel();
		ec.setxAxis(FrameUtil.newArrayList(null));
		ec.setLegend(FrameUtil.newArrayList(getFormateX(timeFrom)));
		ec.addSeries(FrameUtil.newArrayList(null), getFormateX(timeFrom));
		List<List<Long>> searchParam = TimeDimensionUtil.getSearchRanges(
				timeType, timeFrom);
		if(timeType!=TimeType.DAY && ("login_amount".equals(field)||"rech_amount".equals(field)||"invest_amount".equals(field))){
			for (int i = searchParam.size() - 1; i >= 0; i--) {
				ec.getXAxis().add(DateUtil.getFormateStringByTimeDimention(searchParam.get(i).get(1), "yyyy-MM-dd"));
				Integer sum=this.basicDataPcService.getSummary(
						FrameUtil.newHashMap( 
								"start",DateUtil.dateStringToTimestamp(searchParam.get(i).get(0).toString(),DateUtil.PATTERN_yyyyMMdd),
								"end",DateUtil.dateStringToTimestamp( searchParam.get(i).get(1).toString(), DateUtil.PATTERN_yyyyMMdd)+86400,
								"propertyName", field,
								"channel",type==null?"2,3":type,
								"rech_channel","2,3,8,9"));
				ec.getSeries().get(0).getData().add( sum.toString() );
				}
		}else{
			for (int i = searchParam.size() - 1; i >= 0; i--) {
				ec.getXAxis().add(DateUtil.getFormateStringByTimeDimention(searchParam.get(i).get(1), "yyyy-MM-dd"));
				Double sum = ArithUtil.round(this.basicDataAppDao.getFundsSum(
						FrameUtil.newHashMap("field", field, "start", searchParam
								.get(i).get(0), "end", searchParam.get(i).get(1),"type",type),
						false), 2);
				ec.getSeries().get(0).getData()
						.add(NumberUtil.formatNumber(sum, NumberUtil.FORMAT_3));
			}
		}
		return ec;
	}

	public String getFormateX(Long value) {
		return DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(
				value + "", DateUtil.PATTERN_yyyyMMdd),
				DateUtil.PATTERN_yyyy_MM_dd);
	}

	@Override
	public double getFundsSum(String field, String start, String end, String type)
			throws Exception {
		// TODO Auto-generated method stub
		return this.basicDataAppDao.getFundsSum(
				FrameUtil.newHashMap("field", field, "start", start, "end", end,"optionType",type),
				false);
	}

}
